package com.cyt.${moduleName?replace('/', '.')}.service.impl;

import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.cyt.base.constant.BaseConstant;
import com.cyt.base.domain.vo.Result;
import com.cyt.base.domain.vo.ResultList;
import com.cyt.base.utils.BaseUtil;
import com.cyt.base.utils.ResultUtil;
import com.cyt.system.constant.ConfigEnum;
import com.cyt.${moduleName?replace('/', '.')}.dao.${entityName}Dao;
import com.cyt.${moduleName?replace('/', '.')}.domain.dto.${entityName}Dto;
import com.cyt.${moduleName?replace('/', '.')}.domain.po.${entityName};
import com.cyt.${moduleName?replace('/', '.')}.domain.vo.${entityName}Vo;
import com.cyt.${moduleName?replace('/', '.')}.domain.vo.excel.${entityName}ExportVo;
import com.cyt.${moduleName?replace('/', '.')}.domain.vo.excel.${entityName}ImportVo;
import com.cyt.${moduleName?replace('/', '.')}.service.${entityName}Service;
import com.cyt.system.utils.ConfigUtil;
import com.cyt.system.utils.ExcelUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ${description}-service实现层
 *
 * @author ${authorName}
 * @date ${.now}
 */
@Service
public class ${entityName}ServiceImpl implements ${entityName}Service {

    @Resource
    private ${entityName}Dao ${entityName?uncap_first}Dao;

    private MPJQueryWrapper<SysArea> initWrapper(${entityName}Dto ${entityName?uncap_first}Dto) {
        return initWrapper(${entityName?uncap_first}Dto, true);
    }
    private MPJQueryWrapper<${entityName}> initWrapper(${entityName}Dto ${entityName?uncap_first}Dto, Boolean isOrderBy) {

        MPJQueryWrapper<${entityName}> queryWrapper = new MPJQueryWrapper<>();
        queryWrapper.selectAll(${entityName}.class, "t");
        queryWrapper.eq("t.remove_tag", BaseConstant.REMOVE_TAG_NO);
        if (StringUtils.isEmpty(${entityName?uncap_first}Dto.getRemoveTag())) {
            queryWrapper.eq("t.remove_tag", BaseConstant.REMOVE_TAG_NO);
        } else {
            queryWrapper.eq("t.remove_tag", ${entityName?uncap_first}Dto.getRemoveTag());
        }
        if (StringUtils.isNotBlank(${entityName?uncap_first}Dto.getId())) {
            queryWrapper.in("t.id", BaseUtil.toList(${entityName?uncap_first}Dto.getId()));
        }
        if (Boolean.TRUE.equals(isOrderBy)) {
            if (StringUtils.isBlank(${entityName?uncap_first}Dto.getOrderByClause())) {
                queryWrapper.orderByDesc("t.create_time");
            } else {
                if (Boolean.TRUE.equals(${entityName?uncap_first}Dto.getIsAsc())) {
                    queryWrapper.orderByAsc(BaseUtil.getOrderByClause(${entityName}.class, ${entityName?uncap_first}Dto.getOrderByClause()));
                } else {
                    queryWrapper.orderByDesc(BaseUtil.getOrderByClause(${entityName}.class, ${entityName?uncap_first}Dto.getOrderByClause()));
                }
            }
        }
        BaseUtil.wrapperDataScope(queryWrapper, ${entityName?uncap_first}Dto);
        return queryWrapper;
    }

    /**
     * 分页查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return PageInfo<${entityName}>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public PageInfo<${entityName}> findPage(${entityName}Dto ${entityName?uncap_first}Dto) {
        PageHelper.startPage(${entityName?uncap_first}Dto);
        return new PageInfo<>(this.findList(${entityName?uncap_first}Dto));
    }

    /**
     * 分页查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return PageInfo<${entityName}Vo>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public PageInfo<${entityName}Vo> findVoPage(${entityName}Dto ${entityName?uncap_first}Dto) {
        PageHelper.startPage(${entityName?uncap_first}Dto);
        return new PageInfo<>(this.findVoList(${entityName?uncap_first}Dto));
    }

    /**
     * List查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return List<${entityName}>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public List<${entityName}> findList(${entityName}Dto ${entityName?uncap_first}Dto) {
        return ${entityName?uncap_first}Dao.selectList(initWrapper(${entityName?uncap_first}Dto));
    }

    /**
     * List查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return List<${entityName}Vo>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public List<${entityName}Vo> findVoList(${entityName}Dto ${entityName?uncap_first}Dto) {
        return ${entityName?uncap_first}Dao.findVoList(${entityName?uncap_first}Dto);
    }

    /**
     * 对象查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return ${entityName}
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public ${entityName} findOne(${entityName}Dto ${entityName?uncap_first}Dto) {
        return ${entityName?uncap_first}Dao.selectOne(initWrapper(${entityName?uncap_first}Dto));
    }

    /**
     * 对象查询${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return ${entityName}Vo
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public ${entityName}Vo findVoOne(${entityName}Dto ${entityName?uncap_first}Dto) {
        return ${entityName?uncap_first}Dao.findVoOne(${entityName?uncap_first}Dto);
    }

    /**
     * 保存${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @param userId 用户Id信息
     * @return Result<?>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> add(${entityName}Dto ${entityName?uncap_first}Dto, String userId) {

       ${entityName} ${entityName?uncap_first} = new ${entityName}(userId);
<#list table.columns as column>
    <#if column.columnName != "Id" && column.columnName != "RemoveTag" && column.columnName != "Creator" && column.columnName != "CreateTime" && column.columnName != "Updater" && column.columnName != "UpdateTime">
        ${entityName?uncap_first}.set${column.columnName}(${entityName?uncap_first}Dto.get${column.columnName}());
    </#if>
</#list>
        ${entityName?uncap_first}Dao.insert(${entityName?uncap_first});

        return ResultUtil.success();
    }

    /**
    * 更新${description}
    *
    * @param ${entityName?uncap_first}Dto 参数传入对象
    * @param userId 用户Id信息
    * @return Result<?>
    * @author ${authorName}
    * @date ${.now}
    */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> update(${entityName}Dto ${entityName?uncap_first}Dto, String userId) {

        ${entityName} ${entityName?uncap_first} = ${entityName?uncap_first}Dao.selectById(${entityName?uncap_first}Dto.getId());
<#list table.columns as column>
    <#if column.columnName != "Id" && column.columnName != "RemoveTag" && column.columnName != "Creator" && column.columnName != "CreateTime" && column.columnName != "Updater" && column.columnName != "UpdateTime">
        ${entityName?uncap_first}.set${column.columnName}(${entityName?uncap_first}Dto.get${column.columnName}());
    </#if>
</#list>
<#list table.columns as column>
<#if column.columnName != "Id" && column.columnName != "RemoveTag" && column.columnName != "Creator" && column.columnName != "CreateTime">
    <#if column.columnName == "Updater">
        ${entityName?uncap_first}.setUpdater(userId);
    <#elseif column.columnName == "UpdateTime">
        ${entityName?uncap_first}.setUpdateTime(new Date());
    </#if>
</#if>
</#list>
        ${entityName?uncap_first}Dao.updateById(${entityName?uncap_first});
        return ResultUtil.success();
    }

    /**
     * 批量逻辑删除${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @param userId 用户Id信息
     * @return Result<?>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> remove(${entityName}Dto ${entityName?uncap_first}Dto, String userId) {
        //修改删除标志
        ${entityName} ${entityName?uncap_first} = new ${entityName}();
        ${entityName?uncap_first}.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
<#list table.columns as column>
    <#if column.columnName == "Updater">
        ${entityName?uncap_first}.setUpdater(userId);
    <#elseif column.columnName == "UpdateTime">
        ${entityName?uncap_first}.setUpdateTime(new Date());
    </#if>
</#list>
        ${entityName?uncap_first}Dao.update(${entityName?uncap_first}, initWrapper(${entityName?uncap_first}Dto, false));
        return ResultUtil.success();
    }

    /**
     * 批量物理删除${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @return Result<?>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<?> delete(${entityName}Dto ${entityName?uncap_first}Dto) {
        ${entityName?uncap_first}Dao.delete(initWrapper(${entityName?uncap_first}Dto, false));
        return ResultUtil.success();
    }

    /**
     * 下载导入模板
     *
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public void downloadExcel(HttpServletResponse response) {
        ExcelUtil.exportExcel(response, "${description}模板.xlsx", "${description}", new ArrayList<>(), ${entityName}ImportVo.class);
    }

    /**
     * 导入${description}
     *
     * @param file     参数传入对象
     * @param userId 用户Id信息
     * @return ResultList<${entityName}ImportVo>
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultList<${entityName}ImportVo> importExcel(MultipartFile file, String userId) {
        ExcelImportResult<${entityName}ImportVo> excelImportResult = ExcelUtil.importExcelMore(file, ${entityName}ImportVo.class);
        if (excelImportResult == null) {
            return ResultUtil.failList();
        }
        if (excelImportResult.isVerifyFail()) {
            return ResultUtil.failList(excelImportResult.getFailList());
        }
        List<${entityName}ImportVo> ${entityName?uncap_first}ImportVoList = excelImportResult.getList();
        if (CollectionUtils.isNotEmpty(${entityName?uncap_first}ImportVoList)) {
            List<String> idList = new ArrayList<>();
            List<${entityName}> insertList = new ArrayList<>();
            for (${entityName}ImportVo ${entityName?uncap_first}ImportVo : ${entityName?uncap_first}ImportVoList) {
                ${entityName} ${entityName?uncap_first} = new ${entityName}(userId);
        <#list table.columns as column>
            <#if column.columnName != "Id" && column.columnName != "RemoveTag" && column.columnName != "Creator" && column.columnName != "CreateTime" && column.columnName != "Updater" && column.columnName != "UpdateTime">
                ${entityName?uncap_first}.set${column.columnName}(${entityName?uncap_first}ImportVo.get${column.columnName}());
            </#if>
        </#list>
                insertList.add(${entityName?uncap_first});
            }
            if (CollectionUtils.isNotEmpty(idList)) {
                QueryWrapper<${entityName}> wrapper = new QueryWrapper<>();
                wrapper.in("id", idList);
                ${entityName?uncap_first}Dao.delete(wrapper);
            }
            if (CollectionUtils.isNotEmpty(insertList)) {
                ${entityName?uncap_first}Dao.insertList(insertList);
            }
        }
        return ResultUtil.success(new ArrayList<>());
    }

    /**
     * 导出${description}
     *
     * @param ${entityName?uncap_first}Dto 参数传入对象
     * @author ${authorName}
     * @date ${.now}
     */
    @Override
    public void exportExcel(HttpServletResponse response, ${entityName}Dto ${entityName?uncap_first}Dto) {
        ${entityName?uncap_first}Dto.setPageSize(ConfigUtil.getInteger(ConfigEnum.EXPORT_PAGE_SIZE));
        ExcelUtil.exportExcelBig(response, "${description}.xlsx", ${entityName}ExportVo.class, (o, pageNum) -> {
            ${entityName}Dto dto = (${entityName}Dto) o;
            dto.setPageNum((pageNum - 1) * dto.getPageSize());
            return new ArrayList<>(${entityName?uncap_first}Dao.findForExcel(dto));
        }, ${entityName?uncap_first}Dto);
    }
}
